package com.fragment2;


import android.os.Bundle;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.common.base.BaseFragment;
import com.common.event.EventBusUtil;
import com.common.event.MessageEvent;
import com.common.route.RouterPath;
import com.fragment2.databinding.FragmentHomeBinding;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

@Route(path = RouterPath.HOME_FRAGMENT)
public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    @Override
    protected Class<FragmentHomeBinding> getBindingClass() {
        return FragmentHomeBinding.class;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        binding.tvTitle.setText("首页");
        binding.btnSendMessage.setOnClickListener(v -> sendMessage());
        EventBusUtil.register(this);
    }

    @Override
    protected void loadData() {
        // 加载首页数据
    }

    private void sendMessage() {
        MessageEvent event = new MessageEvent();
        event.setMessage("来自首页的消息");
        EventBusUtil.post(event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        binding.tvMessage.setText("收到消息: " + event.getMessage());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBusUtil.unregister(this);
    }

    @Override
    public void onTabReselected() {
        // 回到顶部
        binding.scrollView.fullScroll(View.FOCUS_UP);
    }
}
